/*
 * Copyright 2009 SHOP.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shop.opensocial.mediated.shindig.factories;

import java.util.List;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * Factory for converting between Mediator type and Shindig type
 *
 * @author Jordan Zimmerman
 */
public abstract class GenericMediatorFactory<SHINDIG, MEDIATED> implements GenericMediatorFactoryBase<SHINDIG, MEDIATED>
{
	@Override
	public final SHINDIG make(MEDIATED c)
	{
		return (c != null) ? internalMake(c) : null;
	}

	@Override
	public final MEDIATED unmake(SHINDIG c)
	{
		return (c != null) ? internalUnmake(c) : null;
	}

	@Override
	public List<SHINDIG> makeList(List<MEDIATED> mediatedList)
	{
		List<SHINDIG>		shindigList = new ArrayList<SHINDIG>(mediatedList.size());
		for ( MEDIATED m : mediatedList )
		{
			shindigList.add(internalMake(m));
		}
		return shindigList;
	}

	@Override
	public Set<SHINDIG> makeSet(Set<MEDIATED> mediatedSet)
	{
		Set<SHINDIG> 	shindigSet = new HashSet<SHINDIG>(mediatedSet.size());
		for ( MEDIATED m : mediatedSet )
		{
			shindigSet.add(internalMake(m));
		}
		return shindigSet;
	}

	@Override
	public List<MEDIATED> unmakeList(List<SHINDIG> shindigList)
	{
		List<MEDIATED>		mediatedList = new ArrayList<MEDIATED>(shindigList.size());
		for ( SHINDIG s : shindigList )
		{
			mediatedList.add(internalUnmake(s));
		}
		return mediatedList;
	}
	
	@Override
	public Set<MEDIATED> unmakeSet(Set<SHINDIG> shindigSet)
	{
		Set<MEDIATED> 		mediatedSet = new HashSet<MEDIATED>(shindigSet.size());
		for ( SHINDIG s : shindigSet )
		{
			mediatedSet.add(internalUnmake(s));
		}
		return mediatedSet;
	}

	/**
	 * Does the work of converting between types
	 *
	 * @param c input
	 * @return output
	 */
	protected abstract SHINDIG internalMake(MEDIATED c);

	/**
	 * Does the work of converting between types
	 *
	 * @param c input
	 * @return output
	 */
	protected abstract MEDIATED internalUnmake(SHINDIG c);
}
